package com.example.peachgrove.controller.blog;

import com.example.peachgrove.dao.WorksMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@Api(tags = "前台文章获取")
@RestController
@RequestMapping("/blog/create")
@ResponseBody
public class BlogWorksController {


    @Autowired
    WorksMapper worksMapper;

    @ApiOperation("根据类型id查询列表")
    @GetMapping("/getWorksByTypeId")
    @ApiImplicitParams({
            @ApiImplicitParam(name="typeId",value="类型ID",dataTypeClass = String.class, paramType = "query")
    })
    public HashMap getWorksByTypeId(HttpServletRequest servletRequest){
        HashMap res = new HashMap();
        List datas = worksMapper.getWorksByTypeId(servletRequest.getParameter("typeId"));
        res.put("datas",datas);
        return res;
    }

    @ApiOperation("根据文章id查询文章")
    @GetMapping("/getByWorkId")
    @ApiImplicitParams({
            @ApiImplicitParam(name="workId",value="id",dataTypeClass = Integer.class),
    })
    public HashMap getByWorkId(int workId){
        HashMap res = new HashMap();
        try {
            res.put("data" , worksMapper.getByWorkId(workId));
        }catch (Error e){
            res.put("Error",e);
        }
        return res;
    }

    @ApiOperation("获取所文章")
    @GetMapping("/getAllWorks")
    public HashMap getAllWorks(){
        HashMap res = new HashMap();
        res.put("data",worksMapper.getAllWorks());
        return res;
    }
}